package com.iwish.server.model;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.google.code.morphia.Datastore;
import com.iwish.server.storage.DAO.MongoDAO;
import com.iwish.server.storage.DAO.SQLCommand;
import com.iwish.server.storage.model.Obj;
import com.iwish.server.util.Base64Util;
import com.iwish.server.util.RequestCode;
import com.iwish.server.util.Separator;

public class RegisterService extends ServiceTemplate{

	Logger logger = Logger.getLogger(RegisterService.class);
	private String uName;
	private String password;
//	private String nickName;
//	private boolean gender = true;
//	private String img;
	
	public RegisterService()
	{
		
	}
	
//	public RegisterService(String uName, String password, String nickName, String gender, String img)
	public RegisterService(String uName, String password)
	{
		this.uName = uName;
		this.password = password;
//		this.nickName = nickName;
//		
//		//male true 1 ; female false 0;
//		if("0".equals(gender))
//			this.gender = false;
//		
//		this.img = img;
	}
	
	public boolean register(IoSession session)
	{
		Object u = MongoDAO.execute(Obj.User.class, new SQLCommand() {
			
			@Override
			public Object sql(Class c, Datastore ds) {
				// TODO Auto-generated method stub
				return ds.find(c,"uname_",uName).get();
				
			}
			
			@Override
			public void print() {
				// TODO Auto-generated method stub
				logger.debug("RegisterService - check user exist or not");
				
			}
		});
		if(u != null || ((Obj.User)u).getUname() == uName)
		{
			String result = Base64Util.encode(RequestCode.REGISTER_RESULT.getBytes())
					+ Separator.F_SEPARATOR
					+ Base64Util.encode(RequestCode.SUCCESS.getBytes())
					+ Separator.F_SEPARATOR
					+ "User exist"
					+ Separator.F_SEPARATOR
					//empty uid
					+ Separator.F_SEPARATOR
					;
			session.write(result);
			return true;
		}
		
		//there is no user account exists, allow regist process using this username
		Obj.User.Builder user = Obj.User.newBuilder();
		
		String uid = "u." + uName;
		user.setUid(uid);
		user.setUname(uName);
		user.setPwd(password);
		set(Obj.User.class, uid, user.build());
		
		String result = Base64Util.encode(RequestCode.REGISTER_RESULT.getBytes())
				+ Separator.F_SEPARATOR
				+ Base64Util.encode(RequestCode.FAIL.getBytes())
				+ Separator.F_SEPARATOR
				//empty reason
				+ Separator.F_SEPARATOR
				+ Base64Util.encode(uid.getBytes())
				+ Separator.F_SEPARATOR
				;
		session.write(result);
		return false;
	/*	
		Obj.User.Builder user = Obj.User.newBuilder();
		int index = 0;
		String uid = "u." + uName;
		user.setUid(uid);
		user.setUname(uName);
		user.setPwd(password);
		user.setNick(nickName);
		user.setGender(gender);
		
		//user.setImages(index,img);
		
		set(Obj.User.class, uid,user.build());
		
		String result = Base64Util.encode(RequestCode.REGISTER_RESULT.getBytes())
				+ Separator.F_SEPARATOR
				+ Base64Util.encode(RequestCode.SUCCESS.getBytes())
				+ Separator.F_SEPARATOR
				//empty fail reason
				+ Separator.F_SEPARATOR
				+ Base64Util.encode(uid.getBytes())
				+ Separator.F_SEPARATOR ;
		session.write(result);
		return true;
		*/
	}

	//getters and setters
	public String getuName() {
		return uName;
	}

	public void setuName(String uName) {
		this.uName = uName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

//	public String getNickName() {
//		return nickName;
//	}
//
//	public void setNickName(String nickName) {
//		this.nickName = nickName;
//	}
//
//
//	public boolean isGender() {
//		return gender;
//	}
//
//	public void setGender(boolean gender) {
//		this.gender = gender;
//	}
//
//	public String getImg() {
//		return img;
//	}
//
//	public void setImg(String img) {
//		this.img = img;
//	}
	
	
}
